package com.taotao.rest.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.mapper.TbItemParamMapper;
import com.taotao.po.TbItem;
import com.taotao.po.TbItemDesc;
import com.taotao.po.TbItemDescExample;
import com.taotao.po.TbItemDescExample.Criteria;
import com.taotao.po.TbItemParam;
import com.taotao.po.TbItemParamExample;
import com.taotao.po.TbItemParamItem;
import com.taotao.po.TbItemParamItemExample;
import com.taotao.rest.component.impl.JedisSingleImpl;
import com.taotao.rest.service.ItemService;
import com.taotao.util.JsonUtils;

@Service
public class ItemServiceImpl implements ItemService {

	@Value("${REDIS_ITEM_KEY}")
	private String REDIS_ITEM_KEY;

	@Value("${ITEM_BASE_INFO_KEY}")
	private String ITEM_BASE_INFO_KEY;

	@Value("${ITEM_DESC_KEY}")
	private String ITEM_DESC_KEY;

	@Value("${ITEM_PARAM_KEY}")
	private String ITEM_PARAM_KEY;

	@Value("${ITEM_EXPIRE_SECOND}")
	private Integer ITEM_EXPIRE_SECOND;

	@Autowired
	private TbItemMapper itemMapper;

	@Autowired
	private TbItemDescMapper itemDescMapper;

	@Autowired
	private TbItemParamItemMapper itemParamItemMapper;

	@Resource
	private JedisSingleImpl jedisSingle;

	@Override
	public TbItem getItemById(Long itemId) {

		String json = jedisSingle.get(REDIS_ITEM_KEY + ":" + itemId + ":"
				+ ITEM_BASE_INFO_KEY);
		// 如何缓存有数据 返回该数据
		if (!StringUtils.isBlank(json)) {
			return JsonUtils.jsonToPojo(json, TbItem.class);
		}
		TbItem item = itemMapper.selectByPrimaryKey(itemId);
		if (item==null) {
			return null;
		}
		String itemInfo = JsonUtils.objectToJson(item);
		jedisSingle.set(REDIS_ITEM_KEY + ":" + itemId + ":"
				+ ITEM_BASE_INFO_KEY, itemInfo);
		// 设置key的过期时间
		jedisSingle.expire(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_DESC_KEY,
				ITEM_EXPIRE_SECOND);

		return item;
	}

	@Override
	public TbItemDesc getItemDescById(Long itemId) {
		String json = jedisSingle.get(REDIS_ITEM_KEY + ":" + itemId + ":"
				+ ITEM_DESC_KEY);
		// 如何缓存有数据 返回该数据
		if (!StringUtils.isBlank(json)) {
			return JsonUtils.jsonToPojo(json, TbItemDesc.class);
		}
		TbItemDescExample example = new TbItemDescExample();
		Criteria criteria = example.createCriteria();
		criteria.andItemIdEqualTo(itemId);
		List<TbItemDesc> itemDescList = itemDescMapper
				.selectByExampleWithBLOBs(example);
		if (itemDescList==null||itemDescList.isEmpty()) {
			return null;
		}
		TbItemDesc itemDesc = itemDescList.get(0);
		jedisSingle.set(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_DESC_KEY,
				JsonUtils.objectToJson(itemDesc));
		// 设置key的过期时间
		jedisSingle.expire(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_DESC_KEY,
				ITEM_EXPIRE_SECOND);

		return itemDesc;
	}

	@Override
	public TbItemParamItem getItemParamById(Long itemId) {
		String json = jedisSingle.get(REDIS_ITEM_KEY + ":" + itemId + ":"
				+ ITEM_PARAM_KEY);
		// 如果缓存有数据 返回该数据
		if (!StringUtils.isBlank(json)) {
			return JsonUtils.jsonToPojo(json, TbItemParamItem.class);
		}
		TbItemParamItemExample example =  new TbItemParamItemExample();
		com.taotao.po.TbItemParamItemExample.Criteria criteria = example.createCriteria();
		criteria.andItemIdEqualTo(itemId);
		List<TbItemParamItem> itemParamItemList = itemParamItemMapper.selectByExampleWithBLOBs(example);
		
		// 判断集合是否为空
		if (itemParamItemList==null||itemParamItemList.isEmpty()) {
			return null;
		}
		TbItemParamItem itemParamItem = itemParamItemList.get(0);
		
		// 保存到redis中
		jedisSingle.set(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_PARAM_KEY,
				JsonUtils.objectToJson(itemParamItem));
		// 设置key的过期时间
		jedisSingle.expire(
				REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_PARAM_KEY,
				ITEM_EXPIRE_SECOND);

		return itemParamItem;
	}
}
